package me.siphone.view.grouptree;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.List;
import org.linphone.R;

public class GroupAdapter extends RecyclerView.Adapter<GroupAdapter.GroupViewHolder> {
    private List<Group> groups;
    private OnSelectionChangedListener listener;

    public interface OnSelectionChangedListener {
        void onGroupClick(Group group);

        void onGroupCheck(Group group);
    }

    public GroupAdapter(List<Group> groups, OnSelectionChangedListener listener) {
        this.groups = groups;
        this.listener = listener;
    }

    @NonNull
    @Override
    public GroupViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view =
                LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.item_group, parent, false);
        return new GroupViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull GroupViewHolder holder, int position) {
        Group group = groups.get(position);
        holder.groupName.setText(group.name);
        holder.groupCheckBox.setChecked(group.isChecked);
        holder.itemView.setSelected(group.isSelected);
        holder.groupName.setSelected(group.isSelected);

        holder.itemView.setOnClickListener(
                v -> {
                    for (int i = 0; i < groups.size(); i++) {
                        Group target = groups.get(i);
                        target.isSelected = target.id == group.id;
                        notifyItemChanged(i);
                    }

                    if (listener != null) {
                        listener.onGroupClick(group);
                    }
                });

        holder.groupCheckBox.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        boolean isChecked = ((CheckBox) v).isChecked();
                        groups.get(position).isChecked = isChecked;
                        Log.d(
                                "sisyphus",
                                "设置：" + groups.get(position).name + ", checked: " + isChecked);
                        if (listener != null) {
                            listener.onGroupCheck(group);
                        }
                    }
                });
        holder.groupCheckBox.setOnCheckedChangeListener((buttonView, isChecked) -> {});
    }

    @Override
    public int getItemCount() {
        return groups.size();
    }

    static class GroupViewHolder extends RecyclerView.ViewHolder {
        CheckBox groupCheckBox;
        TextView groupName;

        GroupViewHolder(View itemView) {
            super(itemView);
            groupCheckBox = itemView.findViewById(R.id.groupCheckBox);
            groupName = itemView.findViewById(R.id.groupName);
        }
    }
}
